package com.example.mui.controller;


import com.alibaba.fastjson.JSONArray;
import com.example.mui.domain.User;
import com.example.mui.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-15
 */
@Controller
@RequestMapping("/system/user")
public class UserController
{
    private String prefix = "system/user";
	
	@Autowired
	private IUserService userService;
	
	@GetMapping()
	public String user()
	{
	    return prefix + "/user";
	}
	
	/**
	 * 查询用户列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public String list(User user)
	{
        List<User> list = userService.selectUserList(user);
		System.out.println(list);
		return JSONArray.toJSONString(list);
	}


	/**
	 * 导出用户列表
	 *//*
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(User user)
    {
    	List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "user");
    }
	*/

	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户
	 */
	@PostMapping("/add")
	@ResponseBody
	public String addSave(User user)
	{

		return userService.insertUser(user) > 0 ? "true" : "false";
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
	{
		User user = userService.selectUserById(userId);
		mmap.put("user", user);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户
	 */
	@PostMapping("/edit")
	@ResponseBody
	public String editSave(User user)
	{		
		return userService.updateUser(user) > 0 ? "true" : "false";
	}
	
	/**
	 * 删除用户
	 */
	@GetMapping("/remove/{userId}")
	@ResponseBody
	public String remove(@PathVariable("userId") Long userId)
	{		
		return userService.deleteUserById(userId) > 0 ? "true" : "false";
	}
	
}
